package com.zq.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zq.springcloud.entity.Emp;
import com.zq.springcloud.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ================================================
 * 作    者：程志强
 * 邮    箱：cheng.zhiqiang@cesgroup.com.cn
 * 版    本：1.0.0
 * 创建日期：2020/12/31  10:56
 * 修改日期：
 * 描    述：
 * 修订历史：
 * ================================================
 */
@RestController
@RequestMapping("/provider")
public class EmpController {
    @Autowired
    private IEmpService service;

    @GetMapping("/query-one/{empId}")
    // 代码异常的时候，调转到hystrixGet()方法
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Emp queryById(@PathVariable("empId") long empId) {
        Emp emp = service.queryById(empId);
        if (ObjectUtils.isEmpty(emp)) {
            throw new RuntimeException("empId=>" + empId + ",不存该员工！");
        }
        return emp;
    }


    @GetMapping("/query-list")
    public List<Emp> queryById() {
        return service.queryAll();
    }

    /**
     * 服务熔断方法
     *
     * @param empId
     * @return
     */
    public Emp hystrixGet(@PathVariable("empId") long empId) {
        Emp emp = new Emp();
        emp.setId(empId);
        emp.setEmpName("empId=>" + empId + ",不存该员工！");
        emp.setDataSource("no data in mysql!");
        return emp;
    }
}
